﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using QingzimaLogicModel;
using Jacaranda.Model;
using QingzimaDB;

namespace QingzimaScenario
{
    public class ShuiScenario
    {
        public static ShuiLM HuoquByID(int id)
        {
            return ConvertToLM(Database.SearchOne<ShuiDB>(new AbstractProperty("ID") == id));
        }

        public static ShuiLM HuoquByKey(string key)
        {
            return ConvertToLM(Database.SearchOne<ShuiDB>(new AbstractProperty("Key") == key));
        }

        public static List<ShuiLM> HuoquModels()
        {
            List<ShuiLM> tempLMList = new List<ShuiLM>();
            foreach (ShuiDB dbModel in Database.Select<ShuiDB>("Select * From ShuiDB Order By CreatedTime Desc"))
            {
                tempLMList.Add(ConvertToLM(dbModel));
            }
            return tempLMList;
        }

        public static List<ShuiLM> HuoquModelsByCreatedTime(int shuliang)
        {
            List<ShuiLM> tempLMList = new List<ShuiLM>();
            foreach (ShuiDB dbModel in Database.Select<ShuiDB>("Select top " + shuliang + " * From ShuiDB Order By CreatedTime Desc"))
            {
                tempLMList.Add(ConvertToLM(dbModel));
            }
            return tempLMList;
        }

        public static void Tianjia(ShuiLM ShuiLM)
        {
            ShuiLM.Save();
        }

        public static void Shanchu(string key)
        {
            Database.Delete<ShuiDB>(new AbstractProperty("Key") == key);
        }

        private static ShuiLM ConvertToLM(ShuiDB dbModel)
        {
            return dbModel == null ? null : new ShuiLM(dbModel);
        }

        public static int GetCount()
        {
            //throw new NotImplementedException();

            return Convert.ToInt32(Database.Count<ShuiDB>());
        }
    }
}
